package cn.northsea.controller;

import cn.northsea.pojo.*;
import cn.northsea.service.WarehouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @RequestMapping("/findWarehouseProductAll")
    public String findWarehouseAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                   Model model,
                                   @RequestParam(required = false, defaultValue = "") String likename) {
        PageHelper.startPage(pageNum, pageSize);
        List<WarehouseProductVo> warehouseProductVoList = null;
        PageInfo<WarehouseProductVo> warehouseProductVoPageInfo = null;
        warehouseProductVoList = warehouseService.findWarehouseProductAll(likename);
        warehouseProductVoPageInfo = new PageInfo<>(warehouseProductVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("Page", warehouseProductVoPageInfo);
        return "warehouseProductList";
    }

    @RequestMapping("/findWarehouseMaterialAll")
    public String findWarehouseMaterialAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                           Model model,
                                           @RequestParam(required = false, defaultValue = "") String likename) {
        PageHelper.startPage(pageNum, pageSize);
        List<WarehouseMaterialVo> warehouseMaterialVoList = null;
        PageInfo<WarehouseMaterialVo> warehouseMaterialVoPageInfo = null;
        warehouseMaterialVoList = warehouseService.findWarehouseMaterialAll(likename);
        warehouseMaterialVoPageInfo = new PageInfo<>(warehouseMaterialVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("Page", warehouseMaterialVoPageInfo);
        return "warehouseMaterialList";
    }

    @RequestMapping("/WarehouseOutList")
    public String WarehouseOutList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                   Model model,
                                   @RequestParam(required = false, defaultValue = "") String likename,
                                   @RequestParam(required = false, defaultValue = "") String start,
                                   @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<WarehouseOutVo> warehouseOutVoList = null;
        PageInfo<WarehouseOutVo> warehouseOutVoPageInfo = null;
        warehouseOutVoList = warehouseService.WarehouseOutList(likename, start, end);
        warehouseOutVoPageInfo = new PageInfo<>(warehouseOutVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", warehouseOutVoPageInfo);
        return "warehouseOutList";
    }

    @RequestMapping("/WarehouseInList")
    public String WarehouseInList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                  Model model,
                                  @RequestParam(required = false, defaultValue = "") String likename,
                                  @RequestParam(required = false, defaultValue = "") String start,
                                  @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<WarehouseInVo> warehouseInVoList = null;
        PageInfo<WarehouseInVo> warehouseInVoPageInfo = null;
        warehouseInVoList = warehouseService.WarehouseInList(likename, start, end);
        warehouseInVoPageInfo = new PageInfo<>(warehouseInVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", warehouseInVoPageInfo);
        return "warehouseInList";
    }


    @RequestMapping("/checkMaterialQuantity")
    @ResponseBody
    public String checkMaterialQuantity(Integer materialId, Double materialQuantity) {
        String s = warehouseService.checkMaterialQuantity(materialId, materialQuantity);
        return s;
    }

    @RequestMapping("/confirmOut")
    public String confirmOut(Double materialQuantity, Integer materialId, Integer getMaterialId, Integer pageNum, Model model) {
        warehouseService.updateWarehouseMaterial(materialQuantity, materialId, getMaterialId);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/getMaterial.do";
    }
}
